//
//  HMRefreshControl.swift
//  MicroBlog
//
//  Created by QT on 15/8/4.
//  Copyright © 2015年 綦敦涛. All rights reserved.
//

import UIKit
private let refreshViewOffset: CGFloat = -60
class HMRefreshControl: UIRefreshControl {
    
    override func endRefreshing() {
        super.endRefreshing()
        
        reView.stopLoading()
    }
    
    // MARK: - KVO·
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
//        print(frame)
        if frame.origin.y > 0  {
            return
        }
        if frame.origin.y > refreshViewOffset && reView.rotateFlag == false{

            reView.rotateFlag = true
        }else if frame.origin.y < refreshViewOffset && reView.rotateFlag == true{

            reView.rotateFlag = false
        }
        
        if refreshing {
            reView.startLoading()
        }
        
        
    }
    // MARK: - 构造方法
    override init() {
        super.init()
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepareUI(){
        //KVO 
        self.addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
        
        
        addSubview(reView)
        tintColor = UIColor.clearColor()
        
        reView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: self, size: reView.bounds.size)
        
    }
    deinit{
        self.removeObserver(self, forKeyPath: "frame")
    }
    
    
    // 懒加载刷新视图
    private lazy var reView = HMRefreshView.refreshView()
}




class HMRefreshView: UIView {
    @IBOutlet weak var tipIcon: UIImageView!
    
    @IBOutlet weak var tipView: UIView!
    
    @IBOutlet weak var loadIcon: UIImageView!
    private var rotateFlag = false{
        didSet{
           rotTipIcon()
        }
    }
    
    func rotTipIcon(){
        let angle = rotateFlag ? CGFloat(M_PI - 0.01) : CGFloat(-M_PI + 0.01)
       UIView.animateWithDuration(0.5) { () -> Void in
        self.tipIcon.transform = CGAffineTransformRotate(self.tipIcon.transform, angle)
        }
    }
    // 获取xib视图
    class func refreshView() -> HMRefreshView{
        let refreshview = NSBundle.mainBundle().loadNibNamed("HMRefreshView", owner: nil, options: nil).last as! HMRefreshView
    
        return refreshview
    }
    
    func startLoading(){
        // 开始动画，并不真正刷新
        if loadIcon.layer.animationForKey("loadIconAni") != nil{
            return
        }
        
        
        // 隐藏加载提示视图
        tipView.hidden  = true
        // 旋转视图
        let caf = CABasicAnimation(keyPath: "transform.rotation")
        caf.toValue = 2 * M_PI
        caf.duration = 0.5
        caf.repeatCount = MAXFLOAT
        loadIcon.layer.addAnimation(caf, forKey: "loadIconAni")
    }
    
    func stopLoading(){
        tipView.hidden = false
        loadIcon.layer.removeAllAnimations()
    }
    
}